package org.springframework.remoting.jsonrpc;

import java.io.IOException;
import java.io.Writer;

public class PrototypeAjaxHandler implements AJAXHandler {

  public void writeAjaxCall(Writer writer) throws IOException{
    writer.write("\n");
    writer.write("var AjaxCall = Class.create();\n");
    writer.write("AjaxCall.prototype =\n");
    writer.write("{\n");
    writer.write("  makeCall :function()\n");
    writer.write("  {\n");
    writer.write("    var req = new Ajax.Request(this.url, this.options);\n");
    writer.write("    if (this.options.asynchronous==false){\n");
    writer.write("      return req.transport.responseText;");
    writer.write("    }\n");
    writer.write("  },\n");
    writer.write("  setBody : function(json)\n");
    writer.write("  {\n");
    writer.write("    this.options.postBody = json;\n");
    writer.write("  }\n");
    writer.write("};\n");
    writer.write("Object.extend(AjaxCall.prototype, AjaxInterface.prototype);\n");

  }
  
  public void writeToJson(Writer writer) throws IOException {
    writer.write(" function toJSON(obj){\n");
    writer.write("  return Object.toJSON(obj);");
    writer.write("}\n");
  }

}
